<template>
  <center>
    <h2>单元状态查询</h2>
  </center>
  <center>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" style="width: 500px">
<!--    <el-form-item label="单元编号" prop="unitno">-->
<!--      <el-input v-model="ruleForm.unitno"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="单元编号" prop="unitno">
      <el-select v-model="ruleForm.unitno" placeholder="请选择单元编号" style="width: 400px">
        <el-option
            v-for="item in ruleForm.unitnoList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="单元状态" prop="unitstate">
      <el-input v-model="ruleForm.unitstate" readonly="true"></el-input>
    </el-form-item>
  </el-form>
  </center>

  <center>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('ruleForm')">查询状态</el-button>
      <el-button type="danger" @click="onCancel()">取消</el-button>
    </el-form-item>
  </center>
</template>

<script>
import {get} from "@/utils/http";
import {ElMessage} from "element-plus";

export default {
  name: "CheckUnitState",
  data() {
    return {
      ruleForm: {
        unitno: '',
        unitstate: '',
        unitnoList: []
      },
      rules: {
        unitno:[
          { required: true, message: '请选择单元编号', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  created(){
    this.getOptions();
  },
  methods: {
    getOptions(){
      let that = this;
        get('/getunitnojs', {
          unitno: that.ruleForm.unitno
        }).then(function (res) {
          if (res.code === 1) {
            that.ruleForm.unitnoList = [];
            for(var i=0;i<res.data.length;i++){
              that.ruleForm.unitnoList.push({value: res.data[i].unitno, label: res.data[i].unitno});
            }

            ElMessage.success({
              showClose: true,
              message: "查询成功",
              type: "success",
              center: true
            })
          } else {
            ElMessage.warning({
              showClose: true,
              message: "查询失败",
              type: "warning",
              center: true
            })
          }
        });
    },
    onSubmit(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          get('/checkunitstatejs', {
            unitno: that.ruleForm.unitno
          }).then(function (res) {
            if (res.code === 1) {
              that.ruleForm.unitstate = res.data.unitstate

              ElMessage.success({
                showClose: true,
                message: "查询成功",
                type: "success",
                center: true
              })
            } else {
              ElMessage.warning({
                showClose: true,
                message: "查询失败",
                type: "warning",
                center: true
              })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel(){
      let that = this;
      let router_target = "MainPage";
      that.$router.push({
        name: router_target
      })
      // that.ruleForm.unitstate = that.ruleForm.unitno;
    }
  }
}
</script>

<style scoped>

</style>


